%table{:class=>'index'}
  %tr
    %th หลักสูตร
    %th เกรดรายวิชา (เกรดขั้นต่ำที่ผ่านหลักสูตร)
    %th ใบรับรอง
  - @syllabuses.each do |syllabus|
    %tr{:style=>"background-color:#{cycle('#ddd','#fff')};"}
      %td{:valign=>'top'}= syllabus.name
      %td{:valign=>'top'}
        - syllabus.syllabus_course_mappers.each do |mapper|
          .item{:style=>"clear:both;color:#{cycle('#00f','#0a0')};"}
            .item{:style=>'float:left;width:85%'}
              = mapper.course.name
            .item{:style=>'float:right;text-align:center'}
              - registration_course = RegistrationCourse.find_by_sql("select min(grade) as grade from registration_courses inner join registrations on registration_courses.registration_id = registrations.id and registration_courses.course_id = "+mapper.course.id.to_s+" and registrations.user_id = "+params[:id])
              - if registration_course.present? and registration_course[0].grade.present?
                = registration_course[0].grade
              - else
                = "- "
              - certificate_course_rule = CertificateCourseRule.find_by_sql("select minimum_grade from certificate_course_rules where syllabus_id = "+syllabus.id.to_s+" and course_id = "+mapper.course.id.to_s)
              - if certificate_course_rule.present?
                = "(#{certificate_course_rule[0].minimum_grade})"
      %td{:valign=>'top', :style=>'width:10em;'}
        - certificates = Certificate.find_by_sql("select certificates.ref_code from certificates inner join registrations on certificates.registration_id = registrations.id inner join syllabus_openings on syllabus_openings.id = registrations.syllabus_opening_id inner join syllabuses on syllabuses.id = syllabus_openings.syllabus_id where syllabus_id = "+syllabus.id.to_s+" and registrations.user_id = "+params[:id])
        - if certificates.present?
        - ref_code = certificates.collect(&:ref_code)
          .item= link_to ref_code[0], {:controller=>'syllabus', :action=>'print_certificate', :ref_code=>ref_code[0]}, :target=>'_blank'


